package SunStarView;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.example.administrator.sunstart_library.R;

/**
 * 作者： 孙浩
 * 创建时间： 2017年04月12日9:17
 * 联系方式 QQ：243280864
 * 类注释：
 */

public class ShowPohtoWindow {
    PopupWindow window;
    View PhotoView;
    public interface PhotoPopClickListener{
        void fromCam();
        void fromBox();
    }
    PhotoPopClickListener listener;
    public boolean isShow(){
       return window.isShowing();
    }
    public ShowPohtoWindow(Context context,PhotoPopClickListener listener) {
        this.listener = listener;
        window = new PopupWindow(context);
        ColorDrawable drawable = new ColorDrawable(context.getResources().getColor(R.color.nocolor));
        window.setBackgroundDrawable(drawable);
        setPhotoView(context);
        window.setContentView(PhotoView);
        window.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        window.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        window.setAnimationStyle(R.style.show_pop_ani);
        window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
    }
    public void show(View fatherView){
        window.showAtLocation(fatherView, Gravity.BOTTOM,0,0);
    }
    public void dismiss(){
        window.dismiss();
    }
    public void setDismissListener(final View upView){
        window.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                upView.setVisibility(View.GONE);
            }
        });
    }

    private void setPhotoView(Context context) {
        PhotoView = View.inflate(context, R.layout.photo_popwindow,null);
        TextView photoFromCam = (TextView) PhotoView.findViewById(R.id.take_photo_from_cam);
        TextView photoFromBox = (TextView) PhotoView.findViewById(R.id.take_photo_from_pohtobox);
        TextView photoDismiss = (TextView) PhotoView.findViewById(R.id.take_photo_from_dismiss);
        photoFromCam.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                window.dismiss();
                listener.fromCam();
            }
        });
        photoFromBox.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                window.dismiss();
                listener.fromBox();
            }
        });
        photoDismiss.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                window.dismiss();
            }
        });
    }
}
